package com.furongji.education;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

@Component
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenerSimpleQueueMessage(String msg) {
//        System.out.println(msg);
//    }

    @RabbitListener(queues = "work.queue")
    public void listenerWord1(String msg) throws InterruptedException {
        System.out.println("消费者1收到信息：" + msg + "  " + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work.queue")
    public void listenerWord2(String msg) throws InterruptedException {
        System.out.println("消费者2收到信息：" + msg + "  " + LocalTime.now());
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenerFanoutQ1(String msg) {
        System.out.println("我是队列1：" + msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenerFanoutQ2(String msg) {
        System.out.println("我是队列2：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "test.direct", type = ExchangeTypes.DIRECT),
            key = {"red"}))
    public void listenerDirectQ1(String msg) {
        System.out.println("我是直连队列1：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "test.direct", type = ExchangeTypes.DIRECT),
            key = "red"
    ))
    public void listenerDirectQ2(String msg) {
        System.out.println("我是直连队列2：" + msg);
    }
}
